Friend Class frmPackaging

    Private EH As New ErrorHandler
    Private DBU As DataBaseUtility
    Private m_Confirmed As Boolean
    Private m_TabDelKeyValues

    Private Property TabDelKeyValues() As String
        Get
            Return m_TabDelKeyValues
        End Get
        Set(ByVal value As String)
            Dim TH As New TextHandler
            m_TabDelKeyValues = value
        End Set
    End Property

    Private Property Confirmed() As Boolean
        Get
            Return m_Confirmed
        End Get
        Set(ByVal value As Boolean)
            m_Confirmed = value
        End Set
    End Property

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub frmPackaging_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim DT As New DataTable
        Dim AL As New ArrayList
        Dim TH As New TextHandler

        DBU = New DataBaseUtility

        Try
            AL = TH.ReadFileLinesToArrayList("Packaging.ini")
            DT = DBU.GetPackageData(AL)
            dgvPackages.DataSource = DT
            dgvPackages.Columns(0).Visible = False

            Me.Text = "Packaging For Batch Number " & AL.Item(1).ToString
        Catch ex As Exception
            EH.RecordError(ex, True)
        End Try

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim i As Integer
        Dim AL As New ArrayList
        Dim TH As New TextHandler
        Dim ID, RollNum, Insp, CACD, Yards, Eigths As String

        DBU = New DataBaseUtility

        Try

            AL = TH.ReadFileLinesToArrayList("Packaging.ini")
            TabDelKeyValues = TH.AssembleTabDelString(AL.Item(0).ToString, AL.Item(1).ToString, AL.Item(2).ToString)

            'Update existing data
            For i = 0 To (dgvPackages.Rows.Count - 1)                

                Try

                    Dim Row As New DataGridViewRow
                    Row = dgvPackages.Rows(i)
                    ID = Row.Cells(0).Value.ToString
                    RollNum = Row.Cells(1).Value.ToString
                    Insp = Row.Cells(2).Value.ToString
                    CACD = Row.Cells(3).Value.ToString
                    Yards = Row.Cells(4).Value.ToString
                    Eigths = Row.Cells(5).Value.ToString

                    If DBU.PackingRecordExists(ID) = True Then
                        DBU.PackagingRecordUpdate(ID, RollNum, Insp, CACD, Yards, Eigths)
                    Else
                        DBU.PackagingRecordAdd(TabDelKeyValues, RollNum, Insp, CACD, Yards, Eigths)
                    End If

                Catch ex As Exception
                    'MsgBox(ex.Message)
                End Try

            Next

            'MsgBox(i)

            Me.Close()

        Catch ex As Exception
            EH.RecordError(ex, True)
        End Try

    End Sub

    Private Sub dgvPackages_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvPackages.UserDeletingRow

        Try
            Dim DBU As New DataBaseUtility
            If Confirmed = False Then                
                If MsgBox("Are you sure you want to delete packaging data?", MsgBoxStyle.YesNo, "Confirm Deletion") = MsgBoxResult.Yes Then
                    Confirmed = True
                    DBU.PackingRecordDelete(e.Row.Cells(0).Value.ToString)
                End If
            Else
                DBU.PackingRecordDelete(e.Row.Cells(0).Value.ToString)
            End If
        Catch ex As Exception
            Confirmed = False
            EH.RecordError(ex, True)
        End Try

    End Sub
End Class